package com.kq.myofficialaccount.service.impl;

import com.kq.myofficialaccount.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    // 存储字符串
    public void setString(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    // 存储有过期时间的字符串
    public void setExpireString(String key, String value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    // 获取字符串
    public String getString(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 设置字符串的过期时间
    public void expireString(String key, long timeout, TimeUnit unit) {
        redisTemplate.expire(key, timeout, unit);
    }

    // 删除字符串
    public Boolean deleteString(String key) {
        return redisTemplate.delete(key);
    }
}
